/*This Project Impements the features of List in Python using arrays of C*/

#include<stdlib.h>
#include<stdio.h>
int *a;
int n;

int append(int *a,int n)
{
       int c,i;
       printf("enter the element u want to append : ");
       scanf("%d",&c);
       a[n]=malloc(sizeof(int)*1);
       a[n]=c;
       printf("the array after appending is :);       
       for(i=0;i<=n;i++)
        {
              printf("%d ",a[i]);
        }
       return 0;
}


int extend(int *a)   
{
         int i,m;
          printf("enter the number of elements u want to extend : "); 
         scanf("%d",&m); 
         a=realloc(a,(n+m)*sizeof(int)*1);
         printf("enter the elements u want to extend : "); 
         for(i=0;i<m;i++)
           {  
              scanf("%d",&a[n+i]);
           }
         printf("the array is : ");
         for(i=0;i<n+m;i++)
           {
             printf(" %d",a[i]);
           }
        return 0;
} 


int insert(int *a,int n)
{
       int l,k,i;
       printf("enter the position you want to insert : ");
       scanf("%d",&k);
       a[n]=malloc(sizeof(int)*1);
       printf("enter the value u want to insert : ");
       scanf("%d",&l);
       for(i=n-1;i>=k;i--)
        {
           a[i+1]=a[i];
        }
       a[k]=l;
       printf("the array is : ");
       for(i=0;i<=n;i++)
         {
            printf("%d",a[i]);
         } 
       return 0;
}
      


int indexing(int *a,int n)
{
      int i,l;
      printf("enter the element whose position u want : ");
      scanf("%d",&l);
      printf("the position of the element is :");
      for(i=0;i<n;i++)
       {
          if(a[i]==l)
            { 
               printf("%d",i);   
            }
       
       }
     return 0;
}


int pop(int *a,int n)
{
       int k,i; 
       printf("enter the position of the element: ");
       scanf("%d",&k); 
       a=realloc(a,(n-1)*sizeof(int));   
       printf("the array after popping is : ");
       for(i=0;i<k;i++)
         {
              printf("%d",a[i]);  
         }
       for(i=k+1;i<n;i++)
          {
              printf("%d",a[i]);  
          }
       return 0;
}


int delete(int *a,int n)
{
       int i,j,m,k;
       printf("enter the element u want to delete : ");
       scanf("%d",&k);
       for(i=0;i<n;i++)  
         {
             if(a[i]==k)
                {
                  continue;
                }
              printf("%d",a[i]);
         } 
      return 0;
}


int reverse(int *a)
{
       int i;
       printf("reverse of the array is : ");
       for(i=n-1;i>=0;i--)  
         {
              printf("%d",a[i]);
         }
       return 0;
}


int count(int *a,int n)
{
       int i,count=0;
       for(i=0;i<n;i++)  
         {
             if(a[i]!=0)
              {
                   count=count+1;
              }
         }
        printf("the number of elements are %d: ",count);
        return 0;
}


int sort(int *a,int n)
{
       int i,j,temp,k;
       for(i=0;i<n;i++)
         {  
            for(j=i+1;j<n;j++) 
               {
	          if(a[i]>a[j])
	           {	
           
                       temp=a[j];  
                       a[j]=a[i];
                       a[i]=temp;
           
                   }
	       }
          }
   
        printf("the array after sorting is");
        for(k=0;k<=n-1;k++)
         {
	    printf("%d",a[k]);
	 }
        return 0;
}       

int main()
{
        int c,m,i; 
        printf("enter the number of elements u want to enter :");
        scanf("%d",&n);
        a=malloc(sizeof(int)*n);
        printf("enter the elements : ");
        for(i=0;i<n;i++)
          {
             scanf("%d",&a[i]);
          }
        printf("1.append 2.extend 3.insert 4.indexing 5.pop 6.delete 7.reverse 8.count 9.sort /n");
        scanf("%d",&c);
      switch(c)
   {
       case 1 :append(a,n);
               break;
       case 2 :extend(a);
               break;
       case 3 :insert(a,n);
               break; 
       case 4 :indexing(a,n);
               break;
       case 5 :pop(a,n);
               break;
       case 6 :delete(a,n);
               break;
       case 7 :reverse(a);
               break;   
       case 8 :count(a,n);
               break; 
       case 9 :sort(a,n);
               break; 
  }
   
}

